package edu.wong.pro.serviceImpl;

import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import edu.wong.pro.dao.ActivityDAO;
import edu.wong.pro.dao.AttenderDAO;
import edu.wong.pro.iservice.IActivities;
import edu.wong.pro.model.Activity;
import edu.wong.pro.model.Attender;
import edu.wong.pro.model.AttenderId;

public class ActivitiesService implements IActivities{

	@Override
	public boolean releaseAct(String actTheme, String actContent,
			String actDate, String actAddr, String contacter, String phone) {
		// TODO Auto-generated method stub
		ActivityDAO dao = new ActivityDAO();
		Activity activity  = new Activity();
		activity.setActivityTheme(actTheme);
		activity.setActivityContent(actContent);
		activity.setActivityAddr(actAddr);
		activity.setActivityDate(actDate);
		activity.setActivityContacter(contacter);
		activity.setContactPhone(phone);
		activity.setFucus(0);
		activity.setAttendCount(0);
		Session session = dao.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			dao.save(activity);
			transaction.commit();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}finally{
			session.close();
		}
		return true;
	}

    //获取活动	
	@Override
	public JSONArray getAct() {
		// TODO Auto-generated method stub
	  ActivityDAO dao = new ActivityDAO();
	  return JSONArray.fromObject(dao.findAll());	
	}

	//参与活动
	@Override
	public boolean attends(String actId) {
		// TODO Auto-generated method stub
		HttpSession httpSession = ServletActionContext.getRequest().getSession();
		int memberId = (Integer)httpSession.getAttribute("memberID");
		Integer acti = Integer.parseInt(actId.trim());
		AttenderId id = new AttenderId(acti, memberId);
		AttenderDAO dao = new AttenderDAO();
		Attender attender = dao.findById(id);
		if(attender!=null){
			return true;
		}else {
			Session session = dao.getSession();
			Transaction transaction = session.beginTransaction();
			dao.save(new Attender(id));
			transaction.commit();
			session.close();
			addAttender(acti);		
			return false;
		}
	}

	//增加当前活动的参与人数
	private void addAttender(int id){
		ActivityDAO dao = new ActivityDAO();
		Activity activity = dao.findById(id);
		Session session = dao.getSession();
		Transaction transaction = session.beginTransaction();
		activity.setAttendCount(activity.getAttendCount()+1);
		dao.attachDirty(activity);
		transaction.commit();
		session.close();
	}

	//获取前5条最新的组织活动
	@Override
	public JSONArray getTopAct() {
		// TODO Auto-generated method stub
		Configuration cfg = new Configuration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		Query query = session.createQuery("from Activity order by activityDate desc");
		query.setMaxResults(5);
		List<Activity> list = query.list();
		session.close();
		factory.close();
		return JSONArray.fromObject(list);
	}

	@Override
	public JSONObject getActByID(String actId) {
		// TODO Auto-generated method stub
		int id = Integer.parseInt(actId);
		ActivityDAO dao = new ActivityDAO();
		return JSONObject.fromObject(dao.findById(id));
	}
	
}
